package com.ivy.wechatforclient.fragment;

import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ivy.wechatforclient.R;
import com.ivy.wechatforclient.activity.ConcreteActivity;
import com.ivy.wechatforclient.adapter.ContactAdapter;
import com.ivy.wechatforclient.entity.Friend;
import com.ivy.wechatforclient.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2017/11/17.
 */

public class ContactFragment extends Fragment implements AdapterView.OnItemClickListener{
    private Context context;
    private ListView lv_contact_list;
    private String owner_tel;
    private String path_contact = "http://192.168.49.36:8080/WeChatForServer/FriendServlet";
    private List<Map<String, Object>> allValues = new ArrayList<>();

    public ContactFragment(Context context) {
        this.context = context;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.fragment_contact,null);
        Bundle bundle = getArguments();
        if(bundle != null){
            owner_tel = bundle.getString("owner_tel");
        }

        Thread contactThread = new Thread(new ContectThread());
        contactThread.start();

        lv_contact_list = (ListView) view.findViewById(R.id.lv_contact_list);
        ContactAdapter adapter = new ContactAdapter(context , allValues);

        lv_contact_list.setAdapter( adapter );
        //清空数据
        adapter.clear();

        lv_contact_list.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, Object> friend = allValues.get(position);
        Intent intent = new Intent(context, ConcreteActivity.class);
        intent.putExtra("owner_tel",owner_tel);
        intent.putExtra("telephone", (String) friend.get("telephone"));
        intent.putExtra("username", (String) friend.get("username"));
        startActivity(intent);
    }


    class ContectThread implements Runnable {
        public void run() {
            try {
                Map<String ,String > params = new HashMap<String, String>();
                params.put("owner_tel",owner_tel);
                JSONArray arr = HttpUtil.postJSONArrayByURL(path_contact ,params, "show");
                for(int i = 0;i < arr.length();i++) {
                    JSONObject obj = arr.getJSONObject(i);
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", obj.getInt("id"));
                    map.put("username", obj.getString("username"));
                    map.put("telephone", obj.getString("telephone"));
                  //  map.put("owner_tel", obj.getString("owner_tel"));
                    allValues.add(map);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
